<template>
    <el-container>
        <el-container class="is-vertical">
            <el-header>
                <div class="left-panel">
                    <el-button type="primary" icon="el-icon-plus" @click="table_add" v-auth="'system-admin-add'">增加</el-button>
                </div>
            </el-header>
            <div class="panel-search-box">
                <div class="panel-search">
                    <el-input v-model="search.name" placeholder="姓名" clearable></el-input>
                    <el-input v-model="search.account" placeholder="登录账号" clearable></el-input>
                    <el-input v-model="search.mobile" placeholder="手机号码" clearable></el-input>
                    <el-select v-model="search.identity" placeholder="身份" clearable>
                        <el-option label="普通管理员" :value="1"></el-option>
                        <el-option label="超级管理员" :value="2"></el-option>
                    </el-select>
                    <el-select v-model="search.roleId" placeholder="角色" clearable filterable>
                        <el-option v-for="option in rolelist" :key="option.id" :label="option.name" :value="option.id"></el-option>
                    </el-select>
                    <el-select v-model="search.status" placeholder="状态" clearable>
                        <el-option label="启用" :value="1"></el-option>
                        <el-option label="禁用" :value="0"></el-option>
                    </el-select>

                    <el-button type="primary" icon="el-icon-search" @click="upsearch"></el-button>
                </div>
            </div>
            <el-main class="nopadding">
                <scTable ref="table" :apiObj="$API.system.user.list" remoteSort :params="search" @selection-change="selectionChange">
                    <el-table-column label="头像" width="80">
                        <template #default="scope">
                            <el-avatar :src="scope.row.portrait.url" v-if="scope.row.portrait && scope.row.portrait.url && scope.row" size="small"></el-avatar>
                        </template>
                    </el-table-column>
                    <el-table-column label="姓名" prop="name" width="150"></el-table-column>
                    <el-table-column label="登录账号" prop="account" width="130"></el-table-column>
                    <el-table-column label="手机号" prop="mobile" width="130"></el-table-column>
                    <el-table-column label="身份" prop="identity.name" width="130" align="center"></el-table-column>
                    <el-table-column label="角色" align="left">
                        <template #default="scope">
                            <el-tag size="mini" style="margin-right: 5px;" v-for="item in scope.row.roles" :key="item.id">{{ item.name }}</el-tag>
                        </template>
                    </el-table-column>
                    <el-table-column label="状态" prop="status" width="90" align="center">
                        <template #default="scope">
                            <el-tag size="mini" v-if="scope.row.status.value == 0" type="danger">{{scope.row.status.name}}</el-tag>
                            <el-tag size="mini" v-if="scope.row.status.value == 1" type="success">{{scope.row.status.name}}</el-tag>
                        </template>
                    </el-table-column>
                    <el-table-column label="操作" fixed="right" align="center" width="130">
                        <template #default="scope">
                            <el-button type="text" size="small" @click="table_show(scope.row)">查看</el-button>
                            <el-divider direction="vertical" v-if="$ROLE('system-admin-edit') || $ROLE('system-admin-delete')"></el-divider>
                            <el-dropdown v-if="$ROLE('system-admin-edit') || $ROLE('system-admin-delete')">
                                <el-button type="text" size="small">更多<i class="el-icon-arrow-down el-icon--right"></i></el-button>
                                <template #dropdown>
                                    <el-dropdown-menu>
                                        <el-dropdown-item @click="table_edit(scope.row)" v-auth="'system-admin-edit'">编辑</el-dropdown-item>
                                        <el-popconfirm :title="'确认是否要删除管理员：' + scope.row.name +'?'" @confirm="table_del(scope.row, scope.$index)">
                                            <template #reference>
                                                <el-dropdown-item divided v-auth="'system-admin-delete'">删除</el-dropdown-item>
                                            </template>
                                        </el-popconfirm>
                                    </el-dropdown-menu>
                                </template>
                            </el-dropdown>
                        </template>
                    </el-table-column>
                </scTable>
            </el-main>
        </el-container>
    </el-container>
    <save-dialog v-if="dialog.save" ref="saveDialog" :rolelist="rolelist" @success="handleSuccess" @closed="dialog.save=false"></save-dialog>
    <detail-dialog v-if="dialog.detail" ref="detailDialog" @closed="dialog.detail=false"></detail-dialog>
</template>

<script>
import saveDialog from './save'
import detailDialog from './detail'

export default {
    name: 'user',
    components: {
        saveDialog,
        detailDialog
    },
    data() {
        return {
            dialog: {
                save: false,
                detail: false
            },
            groupFilterText: '',
            group: [],
            selection: [],
            search: {
                name: null,
                mobile: null,
                account: null,
                status: null,
                type: 1
            },
            //角色列表
            rolelist: []
        }
    },
    watch: {
        groupFilterText(val) {
            this.$refs.group.filter(val);
        }
    },
    mounted() {
        this.getrolelist()
    },
    methods: {
        //加载树数据
        async getrolelist() {
            await this.$API.system.role.all.get().then(res => {
                this.rolelist = res.data;
            })
        },
        // 搜索
        upsearch() {
            this.$refs.table.upData(this.search)
        },
        // 增加
        table_add() {
            this.dialog.save = true
            this.$nextTick(() => {
                this.$refs.saveDialog.open()
            })
        },
        // 编辑
        table_edit(row) {
            this.dialog.save = true
            this.$nextTick(() => {
                this.$refs.saveDialog.open('edit').setData(row)
            })
        },
        //查看
        table_show(row) {
            this.dialog.detail = true
            this.$nextTick(() => {
                this.$refs.detailDialog.open('show').setData(row)
            })
        },
        //删除
        async table_del(row) {
            await this.$API.system.user.action.delete({ id: row.id }).then(() => {
                this.handleSuccess()
                this.$notify.success("删除成功")
            })
        },
        //表格选择后回调事件
        selectionChange(selection) {
            this.selection = selection;
        },
        //批量删除
        async batch_del() {
            this.$confirm(`确定删除选中的 ${this.selection.length} 项吗？`, '提示', {
                type: 'warning'
            }).then(() => {
                const loading = this.$loading();
                this.selection.forEach(item => {
                    this.$refs.table.tableData.forEach((itemI, indexI) => {
                        if (item.id === itemI.id) {
                            this.$refs.table.tableData.splice(indexI, 1)
                        }
                    })
                })
                loading.close();
                this.$notify.success("保存成功")
            }).catch(() => {

            })
        },
        //本地更新数据
        handleSuccess() {
            this.$refs.table.refresh()
        }
    }
}
</script>

<style>
</style>
